<?php
namespace app\api\controller\v1;
use think\admin\Controller;
use think\Response;
use app\data\model\DataDevice;
use app\data\model\DataCatToiletLog;
use app\data\model\DataCat;
use think\facade\Db;
/**
 * 图表数据
 * @class Chart
 * @package app\api\controller\v1
 *
 */
class Chart extends Controller
{
    //如厕曲线图
    public function toilet(){
        $id = input('get.id');//设备iotId
        $cat_id = input('get.cat_id');
        $where=[];
        if($id!= ''){
            $where[] = ['l.iot_id','=',$id];
        }
        if($cat_id!= ''){
            $where[] = ['l.cat_id','=',$cat_id];
        }
        $type = input('get.type')?input('get.type'):'0'; //today,week,month
        if($type == '0'){
            $field = 'l.duration as value,c.nickname as name,l.start_time as time,cat_id as id';
            $data['toilet'] = DataCatToiletLog::mk()->alias('l')->field($field)
                ->join('data_cat c','c.id = l.cat_id')
                ->where($where)->whereDay('l.start_time')->select()->toArray();
        }else{
            $days = $type == '1'?7:30;
            $start_time =   date("Y-m-d",strtotime("-{$days} day"))." 00:00:00";
            $end_time =   date("Y-m-d H:i:s");
            $field = 'count(*) as value,c.nickname as name,date_format(l.start_time, "%Y-%m-%d") as time,cat_id as id';
            $data['toilet'] = DataCatToiletLog::mk()->alias('l')->field($field)
                ->join('data_cat c','c.id = l.cat_id')
                ->where($where)
                ->group('date_format(start_time, "%Y-%m-%d"),cat_id')
                ->whereTime('l.start_time', '<=', $end_time)
                ->whereTime('l.start_time', '>=', $start_time)->order('l.start_time asc')
                ->select()->toArray();
        }
        $cats_ids = array_column($data['toilet'] ,'id');
        $cats_ids  = array_unique($cats_ids);
        $map[]=['c.id','in',$cats_ids];
        $cats = DataCat::mk()->getList($map);
        $data['cats'] = $cats;
        $data['count'] = count($data['toilet']);
        $this->success('获取成功',$data);
    }
    //体重曲线图
    public function weight(){
        $id = input('get.id');//设备iotId
        $cat_id = input('get.cat_id');
        $where=[];
        if($id!= ''){
            $where[] = ['l.iot_id','=',$id];
        }
        if($cat_id!= ''){
            $where[] = ['l.cat_id','=',$cat_id];
        }
        $type = input('get.type')?input('get.type'):'0'; //today,week,month
        if($type == '0'){
            $field = 'l.out_weight as value,c.nickname as name,l.start_time as time,cat_id as id';
            $data['weight'] = DataCatToiletLog::mk()->alias('l')->field($field)
                ->join('data_cat c','c.id = l.cat_id')
                ->where($where)->whereDay('l.start_time')->select()->toArray();
        }else{
            $days = $type == '1'?7:90;
            $start_time =   date("Y-m-d",strtotime("-{$days} day"))." 00:00:00";
            $end_time =   date("Y-m-d H:i:s");
            $field = 'l.out_weight as value,c.nickname as name,l.start_time,cat_id as id';
            $subQuery  = DataCatToiletLog::mk()->alias('l')->field($field)
                ->join('data_cat c','c.id = l.cat_id')
                ->where($where)
                ->whereTime('l.start_time', '<=', $end_time)
                ->whereTime('l.start_time', '>=', $start_time)->order('l.start_time desc')
                ->buildSql();
            //echo $subQuery;
            if($cat_id == ''){
                $group = 'date_format(start_time, "%Y-%m-%d"),id';
            }else{
                $group = 'date_format(start_time, "%Y-%m-%d")';
            }
            $data['weight']=Db::table($subQuery . ' a')
                ->field('value,name,date_format(start_time, "%Y-%m-%d") as time,id')
                ->group($group)
                ->order('start_time', 'desc')
                ->select()->toArray();
        }
        $cats_ids = array_column($data['weight'] ,'id');
        $cats_ids  = array_unique($cats_ids);

        $map[]=['c.id','in',$cats_ids];
        $cats = DataCat::mk()->getList($map);
        $data['cats'] = $cats;
        $this->success('获取成功',$data);
    }
}
